package com.eascs.web.o2o.scheduler.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.runtime.RuntimeSetting;
import com.eascs.web.o2o.report.constants.FefundConstants;

@Repository
public class ServiceDao {
	@Inject
	JdbcSession jdbcSessionForService;
	@Inject
	JdbcSession jdbcSessionForOracle;
	@Inject
	JdbcSession jdbcSessionForSlaveService;
	
	@Inject
	JdbcSession jdbcSessionForWorkflow;
	/**
	 * 统计表中数量
	 * 
	 * @param sql
	 * @return
	 */
	public Integer count(String sql,Object...params) {
		return this.jdbcSessionForOracle.findInteger(sql,params);

	}

	/**
	 * 分页查询
	 * 
	 * @param sqlPageQuery
	 * @return
	 */
	public <T> PageResult<T> findDataWithPage(SqlPageQuery<T> sqlPageQuery) {
		return this.jdbcSessionForOracle.findDataWithPage(sqlPageQuery);
	}
	
	public <T> PageResult<T> findDataWithPageForService(SqlPageQuery<T> sqlPageQuery) {
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findDataWithPage(sqlPageQuery);//只有生产环境有主从隔离（从库只读）
		}
		return this.jdbcSessionForService.findDataWithPage(sqlPageQuery);
	}

	public List<Object[]> findParams(String sql,Object...params ){
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findObjectsList(sql, params);//只有生产环境有主从隔离（从库只读）
		}
		return this.jdbcSessionForService.findObjectsList(sql, params);
	}
	
	public List<Object[]> findObjectsList(String sql){
		RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findObjectsList(sql);//只有生产环境有主从隔离（从库只读）
		}
		return this.jdbcSessionForService.findObjectsList(sql);
	}

	/**
	 * s 保存该表数据
	 */
	//@Transactional(transactionManager = "transactionManagerForService")
	public void saveEntity(Object entity) {
		jdbcSessionForService.insert(entity);
	}

	public int updateEntity(Object entity) {
		return jdbcSessionForService.updateById(entity);
	}

	/**
	 * 删除数据
	 */
	public void deleteEntity(String delSql) {
		jdbcSessionForService.execute(delSql);
	}

	/**
	 * @param sql
	 *            
	 */
    //@Transactional(transactionManager = "transactionManagerForService")
	public void excuteSql(String sql, Object... params) {

    	jdbcSessionForService.execute(sql, params);

	}
	
    public <T> List<T> findSimpleObjectList(String sql, Class<T> requiredType, Object... params){
    	RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findSimpleObjectList(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findSimpleObjectList(sql, requiredType, params);
	}
    
	
    public Integer countMysql(String sql,Object... params) {
    	RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findInteger(sql, params);//只有生产环境有主从隔离（从库只读）
		}
		return this.jdbcSessionForService.findInteger(sql, params);

	}
    
    public <T> T findEntity(String sql, Class<T> requiredType, Object... params){
    	RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findEntity(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
    	return jdbcSessionForService.findEntity(sql, requiredType, params);
    }

    public <T> List<T> findEntitys(String sql, Class<T> requiredType, Object... params) {
    	RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findEntitys(sql, requiredType, params);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.findEntitys(sql, requiredType, params);
	}
    
    public <T>  T findById(Class<T> entityClass, String id) {
    	RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.getById(entityClass, id);//只有生产环境有主从隔离（从库只读）
		}
		return jdbcSessionForService.getById(entityClass, id);
	}
    
    public String findString(String sql, Object... params) {
    	RuntimeSetting runtimeSetting = RuntimeSetting.get();
		if(runtimeSetting.getEnv().equals(FefundConstants.ENV_PROD)){
			return jdbcSessionForSlaveService.findString(sql, params);//只有生产环境有主从隔离（从库只读）
		}
    	return jdbcSessionForService.findString(sql, params);
    }
    
    public <T> PageResult<T> findDataWithPageForWorkflow(SqlPageQuery<T> sqlPageQuery) {
		return this.jdbcSessionForWorkflow.findDataWithPage(sqlPageQuery);
	}

    
}
